package org.novelhibernate.core.transaction;

public abstract class TransactionSupport {

	

	protected String convertToMethodName(String name) {

		int start = name.lastIndexOf(".");
		if (-1 == start)
			return nameToMethod(name);
		else
			return name.substring(start + 1);

	}

	protected Class<?> filterToPrimitive(Class<?> clazz) {

		if (Integer.class == clazz)
			return Integer.TYPE;
		else if (Short.class == clazz)
			return Short.TYPE;
		else if (Long.class == clazz)
			return Long.TYPE;
		else if (Boolean.class == clazz)
			return Boolean.TYPE;
		else if (Double.class == clazz)
			return Double.TYPE;
		else if (Float.class == clazz)
			return Float.TYPE;
		else if (Character.class == clazz)
			return Character.TYPE;
		else if (Byte.class == clazz)
			return Byte.TYPE;

		return clazz;
	}

	protected Object nullSafe (Class<?> clazz){
	
		if (Integer.TYPE == clazz)
			return 0;
		else if (Short.TYPE == clazz)
			return 0;
		else if (Long.TYPE == clazz)
			return 0;
		else if (Boolean.TYPE == clazz)
			return false;
		else if (Double.TYPE == clazz)
			return 0.0;
		else if (Float.TYPE == clazz)
			return 0.0;
		else if (Character.TYPE == clazz)
			return "";
		else if (Byte.TYPE == clazz)
			return 0;
		
		return null;
		
	}
	
	/**
	 * extract the name of type from a filed to a path of method (e.g. name to
	 * Name. set or get should be added afterward of course)
	 * 
	 * @param name
	 *            the target name
	 * @return the name with first letter capital
	 */
	protected String nameToMethod(String name) {

		StringBuffer buffer = new StringBuffer(name);
		buffer.setCharAt(0, Character.toUpperCase(buffer.charAt(0)));

		return buffer.toString();
	}
}
